<?php
    header('Content-Type: text/html; charset=utf8');
    session_start();
    include_once '../../../../controlador/php/conexion/Conexion.php';
    $con = new Conexion();
    $dato= $_REQUEST['dat'];
    $filtro =$_REQUEST['dat2'];
    $f1 = $_REQUEST['f1'];
    $f2 = $_REQUEST['f2'];
    
    $fecha1 = date('Y-m-d',strtotime($f1));
    $fecha2 = date('Y-m-d 23:59:00.000',strtotime($f2));
    
    if($filtro == "1"){
        $consulta = "GENESPECI.OID = '".$dato."'";
        
    }else if ($filtro == "2"){
        $consulta = "PACNUMDOC = '".$dato."'";
        
    }else{
        $consulta = "plmfecha Between '".$fecha1."' and '".$fecha2."'";
        
    }
    
    
    $listado= "select ciplmedico.oid,cast(plmpartea as text)as plmpartea,cast(plmparteab as text)as plmparteab,cast(plmpartee as text)plmpartee,
                cast(plmparteeb as text)as plmparteeb,plmfecha, ciplantilla,
                plnombre,pltipoaa,pltipoaab,pltipoaac,cast(plprecargaa as text)as plprecargaa,pltipoaba,pltipoabb,
                cast(plprecargab as text)as plprecargab,pltipoea,pltipoeab,cast(plprecargea as text)as plprecargea,pltipoeba,
                pltipoebb,cast(plprecargeb as text)as plprecargeb,plrutaimgab,plrutaimga,
                ciplmedico.usu_oid,rtrim(usu_nombres+' '+usu_apellidos)as usu_nombres,
                paciente,PACNUMDOC,rtrim(PACPRINOM+' '+PACSEGNOM+' '+PACPRIAPE+' '+PACSEGAPE)as nom_paciente,GPASEXPAC,plespecia,GEEDESCRI
                from ciplmedico 
                inner join ciplantilla on ciplantilla.oid = ciplmedico.ciplantilla
                inner join usuario on ciplmedico.usu_oid = usuario.usu_oid
                inner join ".$con->getReal()."..GENPACIEN on ciplmedico.paciente = GENPACIEN.OID
                inner join ".$con->getReal()."..GENESPECI on ciplantilla.plespecia = GENESPECI.OID
                where ".$consulta."
                order by plmfecha desc";

    $res = $con->query($listado);
      
    while
        ($re = mssql_fetch_array($res)){
        //$lista[] = $re;
       $lista[] = array_map(utf8_encode,$re);
       
    }
    //echo json_encode($lista);
    
    $con->cerrarConexion();
    print_r(json_encode($lista))
    
 
?>
